package com.bos.prop.controller.json;

import com.bos.dao.AnggaranDAO;
import com.bos.dao.KabupatenDAO;
import com.bos.dao.KecamatanDAO;
import com.bos.dao.PeriodeDAO;
import com.bos.domain.Anggaran;
import com.bos.domain.Kabupaten;
import com.bos.domain.Periode;
import com.bos.editor.JenjangSekolahEditor;
import com.bos.editor.KabupatenEditor;
import com.bos.editor.PeriodeEditor;
import com.bos.report.factory.DanaBosKecamatanFactory;
import com.bos.util.UrlUtil;
import com.bos.util.enums.JenjangSekolah;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/17/11
 * Time: 8:29 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class ReportJsonController {

    private PeriodeDAO periodeDAO;
    private KecamatanDAO kecamatanDAO;
    private KabupatenDAO kabupatenDAO;
    private AnggaranDAO anggaranDAO;

    private DanaBosKecamatanFactory danaBosKecamatanFactory;
private RestTemplate restTemplate;
    /*private Credentials credentials;*/


    /* --------------------- Getter and Setter ---------------------*/

    /*public Credentials getCredentials() {
        return credentials;
    }

    @Autowired
    public void setCredentials(Credentials credentials) {
        this.credentials = credentials;
        if (restTemplate != null) {
            CommonsClientHttpRequestFactory factory = (CommonsClientHttpRequestFactory) restTemplate.getRequestFactory();
            HttpClient client = factory.getHttpClient();
            client.getState().setCredentials(AuthScope.ANY, credentials);
        }
    }*/

    public RestTemplate getRestTemplate() {
        return restTemplate;
    }

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;

//        if (credentials != null) {
//            CommonsClientHttpRequestFactory factory = (CommonsClientHttpRequestFactory) restTemplate.getRequestFactory();
//            HttpClient client = factory.getHttpClient();
//            client.getState().setCredentials(AuthScope.ANY, credentials);
//        }
    }

    @Autowired
    public void setAnggaranDAO(AnggaranDAO anggaranDAO) {
        this.anggaranDAO = anggaranDAO;
    }

    @Autowired
    public void setPeriodeDAO(PeriodeDAO periodeDAO) {
        this.periodeDAO = periodeDAO;
    }

    @Autowired
    public void setKabupatenDAO(KabupatenDAO kabupatenDAO) {
        this.kabupatenDAO = kabupatenDAO;
    }

    @Autowired
    public void setKecamatanDAO(KecamatanDAO kecamatanDAO) {
        this.kecamatanDAO = kecamatanDAO;
    }

    @Autowired
    public void setDanaBosKecamatanFactory(DanaBosKecamatanFactory danaBosKecamatanFactory) {
        this.danaBosKecamatanFactory = danaBosKecamatanFactory;
    }

    /**
     * ************* JSON **********************************************************************************************
     */

    @RequestMapping(value = "/report/chart/danaBosKecamatan/kabupaten/{kabupaten}/jenjangSekolah/{jenjangSekolah}/periode/{periode}.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Map createDanaBosKecamatans(@PathVariable("kabupaten")Kabupaten host, @PathVariable("jenjangSekolah")JenjangSekolah jenjangSekolah, @PathVariable("periode") Periode periode) {
        List<Anggaran> anggarans = anggaranDAO.getByJenjangSekolahPeriode(jenjangSekolah, periode);
        danaBosKecamatanFactory.fillDataSource(jenjangSekolah, periode, host.getId(), anggarans);

        //Ambil data dari host kabupaten bersangkutan.
        String hostUrl = UrlUtil.getKabupatenHostUrl(host) + "/report/chart/danaBosKecamatan/jenjangSekolah/{jenjangSekolah}/periode/{periode}.json";
        Map<String, Object> hostUrlVariable = new HashMap<String, Object>();
        hostUrlVariable.put("jenjangSekolah", jenjangSekolah);
        hostUrlVariable.put("periode", periode.getId());

        try {
            ResponseEntity<Map> responseEntity = restTemplate.exchange(hostUrl, HttpMethod.GET, null, Map.class, hostUrlVariable);
            Map response = (Map) responseEntity.getBody();

            //siapkan ModelAndView yang diisi parameter
            danaBosKecamatanFactory.putAllResponseParameter(response);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return danaBosKecamatanFactory.getResponseParameter();
    }

    /**
     * ************** OTHER METHOD *************************************************************************************
     */

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Kabupaten.class, new KabupatenEditor(kabupatenDAO));
        binder.registerCustomEditor(JenjangSekolah.class, new JenjangSekolahEditor());
        binder.registerCustomEditor(Periode.class, new PeriodeEditor(periodeDAO));
    }
}
